{
    "cells": [
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "bcddce7b",
            "metadata": {
                "scrolled": true
            },
            "outputs": [],
            "source": [
                "# download presidio\n",
                "!pip install presidio_analyzer presidio_anonymizer\n",
                "!python -m spacy download en_core_web_lg"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "3345f1c4",
            "metadata": {},
            "source": [
                "###### Path to notebook: [https://www.github.com/microsoft/presidio/blob/main/docs/samples/python/encrypt_decrypt.ipynb](https://www.github.com/microsoft/presidio/blob/main/docs/samples/python/encrypt_decrypt.ipynb)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "gothic-trademark",
            "metadata": {},
            "source": [
                "# Encrypting and Decrypting identified entities\n",
                "\n",
                "This sample shows how to use Presidio Anonymizer built-in functionality, to encrypt and decrypt identified entities.\n",
                "The encryption is using AES cypher in CBC mode and requires a cryptographic key as an input for both the encryption and the decryption.\n"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "roman-allergy",
            "metadata": {},
            "source": [
                "### Set up imports"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 2,
            "id": "extensive-greensboro",
            "metadata": {},
            "outputs": [],
            "source": [
                "from presidio_anonymizer import AnonymizerEngine, DeanonymizeEngine\n",
                "from presidio_anonymizer.entities import RecognizerResult, OperatorResult, OperatorConfig\n",
                "from presidio_anonymizer.operators import Decrypt"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "091be4b6",
            "metadata": {},
            "source": [
                "### Define a cryptographic key (for both encryption and decryption)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "id": "50bc451e",
            "metadata": {
                "collapsed": false,
                "jupyter": {
                    "outputs_hidden": false
                },
                "pycharm": {
                    "name": "#%%\n"
                }
            },
            "outputs": [],
            "source": [
                "crypto_key = \"WmZq4t7w!z%C&F)J\""
            ]
        },
        {
            "cell_type": "markdown",
            "id": "metropolitan-atlantic",
            "metadata": {},
            "source": [
                "### Presidio Anonymizer: Encrypt"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "id": "medium-ridge",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "text: My name is M4lla0kBCzu6SwCONL6Y+ZqsPqhBp1Lhdc3t0FKnUwM=.\n",
                            "items:\n",
                            "[\n",
                            "    {'start': 11, 'end': 55, 'entity_type': 'PERSON', 'text': 'M4lla0kBCzu6SwCONL6Y+ZqsPqhBp1Lhdc3t0FKnUwM=', 'operator': 'encrypt'}\n",
                            "]"
                        ]
                    },
                    "execution_count": 4,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "engine = AnonymizerEngine()\n",
                "\n",
                "# Invoke the anonymize function with the text,\n",
                "# analyzer results (potentially coming from presidio-analyzer)\n",
                "# and an 'encrypt' operator to get an encrypted anonymization output:\n",
                "anonymize_result = engine.anonymize(\n",
                "    text=\"My name is James Bond\",\n",
                "    analyzer_results=[\n",
                "        RecognizerResult(entity_type=\"PERSON\", start=11, end=21, score=0.8),\n",
                "    ],\n",
                "    operators={\"PERSON\": OperatorConfig(\"encrypt\", {\"key\": crypto_key})},\n",
                ")\n",
                "\n",
                "anonymize_result"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "id": "2f8be6b5",
            "metadata": {
                "collapsed": false,
                "jupyter": {
                    "outputs_hidden": false
                },
                "pycharm": {
                    "name": "#%%\n"
                }
            },
            "outputs": [],
            "source": [
                "# Fetch the anonymized text from the result.\n",
                "anonymized_text = anonymize_result.text\n",
                "\n",
                "# Fetch the anonynized entities from the result.\n",
                "anonymized_entities = anonymize_result.items"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "obvious-fifty",
            "metadata": {},
            "source": [
                "### Presidio Anonymizer: Decrypt"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "id": "outstanding-celebration",
            "metadata": {
                "pycharm": {
                    "name": "#%%\n"
                }
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "text: My name is James Bond.\n",
                            "items:\n",
                            "[\n",
                            "    {'start': 11, 'end': 21, 'entity_type': 'PERSON', 'text': 'James Bond', 'operator': 'decrypt'}\n",
                            "]"
                        ]
                    },
                    "execution_count": 8,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "# Initialize the engine:\n",
                "engine = DeanonymizeEngine()\n",
                "\n",
                "# Invoke the deanonymize function with the text, anonymizer results\n",
                "# and a 'decrypt' operator to get the original text as output.\n",
                "deanonymized_result = engine.deanonymize(\n",
                "    text=anonymized_text,\n",
                "    entities=anonymized_entities,\n",
                "    operators={\"DEFAULT\": OperatorConfig(\"decrypt\", {\"key\": crypto_key})},\n",
                ")\n",
                "\n",
                "deanonymized_result"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "id": "9ff6810b",
            "metadata": {
                "collapsed": false,
                "jupyter": {
                    "outputs_hidden": false
                },
                "pycharm": {
                    "name": "#%%\n"
                }
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "'James Bond'"
                        ]
                    },
                    "execution_count": 9,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "# Alternatively, call the Decrypt operator directly:\n",
                "\n",
                "# Fetch the encrypted entitiy value from the previous stage\n",
                "encrypted_entity_value = anonymize_result.items[0].text\n",
                "\n",
                "# Restore the original entity value\n",
                "Decrypt().operate(text=encrypted_entity_value, params={\"key\": crypto_key})"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3 (ipykernel)",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.7.10"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 5
}
